import jsrsasign from 'jsrsasign';
import CryptoJS from 'crypto-js';
// 你的私钥字符串
const privateKeyPem = `-----BEGIN PRIVATE KEY-----
MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBALt2yY6SmshIEFlpbuepE0GQd3K8OrfI+v99PKh/TeCTYXBDZxkFs0vZDlYw2/oFIDDr5Q/SdlUfYoExUzZikfx1P52/bnxFE2b6WBrVRAiKt/Y4Smky5woxoUngoERJw2Ek5vecIFxXncVOFFwOsWtSYgsUCW61EMLWyDVl7ju/AgMBAAECgYBGPCkj+5V2OFm3xiLBNL18MQ4vtIojVW+EzX0ufBPHcjBtiTNVUqdsYXRXopKoR8dZzOfTV7zyA4I6eSp+E8wxr14q2bx+DRmt4kInt0j8ZyFJermyz2BqX/zl95sugAMULoomb4GFrfHjtResDZyG2LwgFXTG1/yZfn2ri88fEQJBAO3eXbud8N2IBZZqy+A2iUxGBsVtdSFxmlm0HlZm3yNV5l8vTYDmo+mknXsjJZgYPghtMbAvIu1Y71hVwcx4pXUCQQDJwNqs2QiVUUFyGx7bhcZOa8G9sEsSh79eaJfxuLdgwU13BL6A0R1L19Ecc7al4neHmw1Lh8K7m954uLOZg9HjAkBSjNFKw01MFGCcCwQmE8ciSUsgSRRVPu+qIfBFW/4kbTM95VSoX2KTOyXMDi6F0iP1GLAdzL36UNr/dQOEynnhAkBoDIz6jafOkixlc7UzU8eVFB/D+1KMSPQZ9Wk/YZs5TBfuZMT23GH2hmQy/z5YSPaKOh3eMTLt3JIcwmzlpEUvAkEApp7GmFQNgMifsz/e+7z0C9fNyQimshH1bQ3pEeFHYuqTjNkw3B7ojAMrnNIlLbo/TOOWgjYR97Sv0Ywo8LAKOA==
-----END PRIVATE KEY-----`;
export default {
	autograph(message) {
		// 创建RSA密钥对象
		const key = jsrsasign.KEYUTIL.getKey(privateKeyPem);

		// 创建SHA1哈希对象
		const md = new jsrsasign.KJUR.crypto.MessageDigest({
			alg: 'sha1',
			prov: 'cryptojs'
		});
		md.updateString(message);

		// 对哈希进行签名
		const signature = new jsrsasign.KJUR.crypto.Signature({
			alg: 'SHA1withRSA',
			prov: 'cryptojs/jsrsa'
		});
		signature.init(key);
		signature.updateString(message);
		const sigValueHex = signature.sign();

		// 将签名转换为Base64编码
		const signatureBase64 = jsrsasign.hextob64(sigValueHex);

		return signatureBase64;
	},

	encryptBase64(data) {
		// AES密钥和初始化向量IV，确保与您JavaScript中的密钥和IV长度相同
		const key = 'rw9ptc2y3gszxj3d'; // 用您的密钥替换 'your-secret-key'
		const iv = 'dg93rvmqu8nrfu88'; // 用您的IV替换 'your-iv'

		// 将密钥和IV编码为字节
		const keyBytes = CryptoJS.enc.Utf8.parse(key);
		const ivBytes = CryptoJS.enc.Utf8.parse(iv);

		// 创建一个新的AES加密对象
		const cipher = CryptoJS.AES.encrypt(data, keyBytes, {
			iv: ivBytes,
			mode: CryptoJS.mode.CBC,
			padding: CryptoJS.pad.Pkcs7
		});

		// 将加密后的字节转换为Base64编码的字符串
		const encryptedBase64 = cipher.toString();

		return encryptedBase64;
	}
}